package com.icode.forkJoin;

import java.util.concurrent.ExecutionException;
import java.util.concurrent.ForkJoinPool;
import java.util.concurrent.ForkJoinTask;
import java.util.stream.LongStream;
import java.util.stream.Stream;

/**
 * @Author: my_code
 * @Email: my_code@foxmail.com
 * @Create: 2020-12-13 17:47
 */
public class Test {

    public static void main(String[] args) throws ExecutionException, InterruptedException {


        long start = System.currentTimeMillis();
        Long sum = 0L;
        for (Long i = 1L; i <= 10_0000_0000; i++) {
            sum += i;
        }
        long end = System.currentTimeMillis();
        System.out.println("sum = " + sum + "     耗时：" + (end - start));


        start = System.currentTimeMillis();
        ForkJoinPool forkJoinPool = new ForkJoinPool();
        ForkJoinTask<Long> submit = forkJoinPool.submit(new ForkJoinDemo(1L, 10_0000_0000L));
        sum = submit.get();
        end = System.currentTimeMillis();
        System.out.println("sum = " + sum + "     耗时：" + (end - start));


        start = System.currentTimeMillis();
        LongStream.rangeClosed(1L, 10_0000_0000L).parallel().reduce(0, Long::sum);
        end = System.currentTimeMillis();
        System.out.println("sum = " + sum + "     耗时：" + (end - start));


    }
}
